<template>
	<view>

		<st-mc-framework :navTitle="ModuleTitle" @changeLevel="changeLevel"
			@AddModuleData_select='AddModuleData_select'>
			<template #workbench>
				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0 }" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="120">
					<template #BtnContent>
						<u-icon name="search" color="#fff" size="25px" @click="search"></u-icon>
					</template>
				</m-drag-button>

				<view class="TabsBox">
					<view class="TabsBox_view">
						<view class="u-page__tag-item" v-for="(item, index) in radios" :key="index">
							<u-tag :text="item.name" :plain="!item.checked" :name="index" size="large"
								@click="radioClick(item,index)">
							</u-tag>
						</view>
					</view>
				</view>
			</template>

			<!-- 主内容 -->
			<template #content>
				<view class="content_box f-dc">
					<view class="table table_top_th">
						<table>
							<tr>
								<th width='16%'>单据编号</th>
								<th width='10%'>制单日期</th>
								<th width='16%'>产品名称</th>

								<th width='10%'>计划数量</th>
								<th width='10%'>可报工数量</th>
								<th width='10%'>完成数量</th>
								<th width='10%'>合格数量</th>
								<th width='10%'>不良数量</th>
								<!-- <th width='12%'>机台</th> -->
								<th width='8%'>生产状态</th>
							</tr>
						</table>
					</view>
					<z-paging ref="paging" v-model="contents" @query="queryList" :fixed="false" class="z_paging">
						<view class="table" style="overflow-x: auto;width: 100%;">
							<table>
								<tbody>
									<tr v-for="(item,index) in contents" @click="planIDClick(item)">
										<td width='16%'>
											<view class="newlineDiv"><text>{{item.PGBillNo}}</text></view>
										</td>
										<td width='10%'>
											<view class="newlineDiv"><text>{{item.BillDate}}</text></view>
										</td>
										<td width='16%'>
											<view class="newlineDiv"><text>{{item.ProductName}}</text></view>
										</td>
										<!-- <td width='7%'>
											<view class="newlineDiv"><text>{{item.ProcedureName}}</text></view>
										</td> -->
										<td width='10%'>
											<view class="ListTdDiv"><text>{{item.FPlanQty}}</text></view>
										</td>
										<td width='10%'>
											<view class="ListTdDiv"><text>{{item.ReportQty}}</text></view>
										</td>
										<td width='10%'>
											<view class="ListTdDiv"><text>{{item.WCQty}}</text></view>
										</td>
										<td width='10%'>
											<view class="ListTdDiv"><text>{{item.FDailyQtt}}</text></view>
										</td>
										<td width='10%'>
											<view class="ListTdDiv"><text>{{item.FNGQty}}</text></view>
										</td>
										<!-- <td width='12%'>
											<view class="newlineDiv"><text>{{item.DeviceName }}</text></view>
										</td> -->

										<td width='8%' :class="rowClass(item.FStateName)">
											<view class="newlineDiv"><text>{{item.FStateName}}</text></view>
										</td>

									</tr>
								</tbody>
							</table>

						</view>

					</z-paging>

				</view>

				<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="10px 10px 0 0"
					@change="changePopup">
					<view class="popup_content">

						<u-cell-group :border='false'>


							<u-cell title="开始时间" :isLink="false" :value='FormData.FBDate'
								@click="addTime('开始')"></u-cell>
							<u-cell title="结束时间" :isLink="false" :value='FormData.FEDate'
								@click="addTime('结束')"></u-cell>
						</u-cell-group>
						<view class="bottomBtns">
							<button block @click="submit">
								查 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 询
							</button>
						</view>

					</view>
				</uni-popup>
				<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show = false'
					@confirm='pickerConfirm'></u-datetime-picker>




			</template>

		</st-mc-framework>

		<u-toast ref="uToast"></u-toast>
		<u-modal :show="modalshow" :title="modaltitle" width="400" @confirm='modalconfirm'>
			<view class="slot-content">
				{{modalcontent}}
			</view>
		</u-modal>
		<m-loading :text="loadingName" v-model="showMLoading" :showClose="false"
			maskBackgroundColor="rgba(0,0,0,0.3)"></m-loading>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';

	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {
				ModuleTitle: '', //标题名称
				showMLoading: false, //是否加载
				loadingName: '正在加载中……', //加载名称
				modalshow: false, //提示显示
				modaltitle: '', //提示名称
				modalcontent: '', //提示内容
				BtnShow: false, //按钮显示
				msgUrl: '',
				msgobj: {},
				msgcontent: '',
				modalcontent: '',
				radios: [{
						name: '全部',
						id: '',
						checked: true
					}, {
						name: '未报工',
						id: '0',
						checked: false
					},
					{
						name: '部分报工',
						id: '2',
						checked: false
					},
					{
						name: '已完成',
						id: '1',
						checked: false
					},

					{
						name: '指定完成',
						id: '-1',
						checked: false
					},


				],
				FormData: {
					PageIndex: 1,
					PageSize: 30,
					FState: '',
					FBDate: '',
					FEDate: '',
					EquipmentName: '',
					EquipmentCode: '',
					EquipmentID: 0,



				},
				contents: [],
				show: false,
				value1: Number(new Date()),
				TimeName: '',
				disabled: false,
			};
		},
		onReady() {
			var that = this
			uni.hideLoading();
			that.showMLoading = true
			const {
				GuyueModule,
				UserInfo,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;
			that.ModuleTitle = appConfigInfo.ActiveModule.ModuleName

			that.$nextTick(() => {
				setTimeout(() => {
					that.BtnShow = true;
				}, 500)
			})
		},
		onLoad(options) {
			var that = this
			that.$safeStatus({
				payload: '其他', // 要传递的数据
			});
			const {
				GuyueModule,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;
			if (options.Dispatchitem) {
				const dispatchItem = JSON.parse(options.Dispatchitem);
				that.assignFormData(that.FormData, dispatchItem);
				return;
			}
			const formDefaults = {
				FBDate: getBefore7DateTime(),
				FEDate: getFormatDateTime(new Date()),
				EquipmentID: MesBasicInfo.warehouse.EquipmentID,
				EquipmentName: MesBasicInfo.warehouse.EquipmentName,
			};
			that.assignFormData(that.FormData, formDefaults);
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		onShow() {
			var that = this
			// this.clearHovered();
			uni.$on('refreshList', () => {
				this.getList(); // 调用目标方法
			});
		},
		onUnload() {
			uni.$off('refreshList');
		},
		computed: {
			// 计算属性返回一个函数
			rowClass() {
				return (status) => ({
					'inactive-row-yellow': status === '部分报工',
					'inactive-row-green': status === '已完成',
					'inactive-row-red': status === '未报工'
				});
			}
		},
		methods: {
			// 创建表单赋值工具函数
			assignFormData(target, defaults = {}) {
				var that = this;
				Object.keys(defaults).forEach(key => {
					target[key] = defaults[key];
				});
				that.$forceUpdate();
				console.log(that.FormData);
			},
			showModal(title, content) {
				this.modaltitle = title;
				this.modalcontent = content;
				this.modalshow = true;
			},
			modalconfirm() {
				var that = this
				that.modalshow = false
			},
			GonavigateTo(obj, url) {
				let that = this;
				var initData = JSON.parse(JSON.stringify(that.$store.state.GuyueModule.ModuleData_select));
				var parentData = initData[initData.length - 1]
				parentData.url = `/st_mes_zs/production/reportinglist?Dispatchitem=${JSON.stringify(that.FormData)}`;
				that.$modifyParentData(parentData);

				this.$safeNavigate({
					targetUrl: url, // 目标页面路径
					payload: obj, // 要传递的数据
				});
			},
			async AddModuleData_select(res) {
				try {
					const backDelta = await this.GetModuleData_select(res);
				} catch (error) {
					console.error('返回操作失败:');
				}
			},




			getList() {
				console.log('触发');
				this.contents = []; // 假设这是异步获取数据的操作
				this.$nextTick(() => {
					this.$refs.paging.reload();
				})
			},



			scan() {
				let that = this;

				uni.scanCode({
					success: function(res) {
						that.jobid = res.result.replace(/^.*?\|/, '')
						var obj = {
							JobOrderID: that.jobid,
							EquipmentID: that.FormData.EquipmentID,
							Type: 'BG'
						}
						console.log(obj);
						that.Request.request('scanJobOrder', obj, (ret) => {
							console.log(JSON.stringify(ret));
							if (ret.status == 200) {
								if (ret.data.ds.length > 0) {
									if (ret.data.ds[0].status == 500) {

										that.showModal('报错', ret.data.ds[0].msg);

									} else {
										var item = ret.data.ds[0]
										item.FDailyOutputID = 0
										var url
										let routes = getCurrentPages();
										url =
											`/st_mes_zs/production/reportingDetail?item=${JSON.stringify(item)}`
										let obj = {
											path: 'manual',
											Name: '报工详情',
											APPFunID: '报工详情',
											length: routes.length + 1,
											url: url,
										}

										if (item.msg == '') {
											that.GonavigateTo(obj, url)
										} else {
											that.msgobj = obj
											that.msgUrl = url
											that.showModal('提醒', item.msg);



										}
									}

								} else {
									that.showModal('提示', '该派工单的线体和当前线体对应不上 不能扫码');

								}


							}
						})

					}
				});

			},
			planIDClick(item) {
				let that = this;

				var msgShow = false

				var url
				let routes = getCurrentPages();
				url = `/st_mes_zs/production/reportingDetail?item=${JSON.stringify(item)}`
				let obj = {
					path: 'manual',
					Name: '报工详情',
					APPFunID: '报工详情',
					length: routes.length + 1,
					url: url,
				}
				if (item.msg == '') {
					that.GonavigateTo(obj, url)
				} else {
					that.msgobj = obj
					that.msgUrl = url
					that.showModal('提醒', item.msg);
				}

			},

			addTime(name) {
				this.TimeName = name
				if (this.TimeName == '开始') {
					this.value1 = Number(getTimeStamp(this.FormData.FBDate))
				} else {
					this.value1 = Number(getTimeStamp(this.FormData.FEDate))
				}
				this.show = true
			},
			search() {
				this.$refs.popup.open('bottom')
			},
			submit() {
				let that = this;


				this.$refs.popup.close('bottom')
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			pickerConfirm(e) {
				let that = this;
				var time = getFormatDateTime(e.value)
				if (this.TimeName == '开始') {
					this.FormData.FBDate = time
				} else {
					this.FormData.FEDate = time
				}
				this.show = false
			},
			changePopup() {

			},
			radioClick(item, idx) {
				let that = this;
				this.radios.map((item, index) => {
					item.checked = index === idx ? true : false
				})
				that.FormData.FState = item.id;
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			queryList(pageNo) {
				let that = this;
				that.FormData.PageIndex = pageNo
				try {
					console.log(that.FormData);
					that.Request.request('getWOJobOrderList', that.FormData, (ret) => {
						console.log(ret);
						that.showMLoading = false
						if (ret.status == 200) {
							ret.data.ds.map(item => item.BillDate = formatDateTime(item.BillDate))
							that.$refs.paging.complete(ret.data.ds);
						}
					}, (err) => {
						that.showMLoading = false
					})
				} catch (e) {
					console.log(e)
					that.showMLoading = false
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},

			changeLevel(list, data, row) {
				getchangeLevel(data)

			},


		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";

	.inactive-row-yellow text {
		color: #fcc105;
	}

	.inactive-row-red text {
		color: red;
	}

	.inactive-row-green text {
		color: green;
	}
</style>